//
//  registerViewController.swift
//  HinsLive
//
//  Created by haoxian on 2019/4/20.
//  Copyright © 2019 Hins. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import SVProgressHUD
import Toast


class RegisterViewController: ViewController {
    
    @IBOutlet weak var accountTF: UITextField! {
        didSet {
            accountTF.rx.text.orEmpty.map { $0.count <= 50 }
                .subscribe(onNext: { [weak self] (enable) in
                    if enable { return }
                    self?.accountTF.deleteBackward()
                }).disposed(by: bag)
        }
    }
    @IBOutlet weak var accountStateLabel: UILabel!
    @IBOutlet weak var passwordTF: UITextField! {
        didSet {
            passwordTF.rx.text.orEmpty.map { $0.count <= 100 }
                .subscribe(onNext: { [weak self] (enable) in
                    if enable { return }
                    self?.passwordTF.deleteBackward()
                }).disposed(by: bag)
        }
    }
    @IBOutlet weak var passwordStateLabel: UILabel!
    @IBOutlet weak var repeatTF: UITextField! {
        didSet {
            repeatTF.rx.text.orEmpty.map { $0.count <= 100 }
                .subscribe(onNext: { [weak self] (enable) in
                    if enable { return }
                    self?.repeatTF.deleteBackward()
                }).disposed(by: bag)
        }
    }
    @IBOutlet weak var repeatStateLabel: UILabel!
    @IBOutlet weak var nameTF: UITextField! {
        didSet {
            nameTF.rx.text.orEmpty.map { $0.count <= 50 }
                .subscribe(onNext: { [weak self] (enable) in
                    if enable { return }
                    self?.nameTF.deleteBackward()
                }).disposed(by: bag)
        }
    }
    @IBOutlet weak var nameStateLabel: UILabel!
    @IBOutlet weak var loginBtn: UIButton! {
        didSet {
            loginBtn.layer.cornerRadius = 3
            loginBtn.layer.masksToBounds = true
            loginBtn.rx.tap.subscribe(onNext: { [weak self] _ in
                self?.navigationController?.popViewController(animated: true)
            }).disposed(by: bag)
        }
    }
    @IBOutlet weak var registerBtn: UIButton! {
        didSet {
            registerBtn.layer.cornerRadius = 3
            registerBtn.layer.masksToBounds = true
        }
    }
    
    private lazy var viewModel = RegisterViewModel(accountDriver: accountTF.rx.text.orEmpty.asDriver(),
                                                   passwordDriver: passwordTF.rx.text.orEmpty.asDriver(),
                                                   repeatDriver: repeatTF.rx.text.orEmpty.asDriver(),
                                                   nameDriver: nameTF.rx.text.orEmpty.asDriver())

    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewModel.accountValid.bind(to: accountStateLabel.rx.isValid).disposed(by: bag)
        viewModel.passwordValid.bind(to: passwordStateLabel.rx.isValid).disposed(by: bag)
        viewModel.repeatValid.bind(to: repeatStateLabel.rx.isValid).disposed(by: bag)
        viewModel.nameValid.bind(to: nameStateLabel.rx.isValid).disposed(by: bag)
        viewModel.registerEnable.bind(to: registerBtn.rx.isEnableTap).disposed(by: bag)
        
        registerBtn.rx.tap
            .map { SVProgressHUD.show() }
            .withLatestFrom(Observable.combineLatest(accountTF.rx.text.orEmpty.asObservable(),
                                                 passwordTF.rx.text.orEmpty.asObservable(),
                                                 nameTF.rx.text.orEmpty.asObservable(),
                                                 resultSelector: { ($0, $1, $2) }))
            .flatMap(viewModel.registerTap)
            .subscribe(onNext: { [weak self] _ in
                self?.view.endEditing(true)
                SVProgressHUD.dismiss()
                self?.navigationController?.dismiss(animated: true)
            }) { [weak self] (error) in
                SVProgressHUD.dismiss()
                self?.view.makeCenterMsg(error.localizedDescription)
            }.disposed(by: bag)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        view.endEditing(true)
    }

    @IBAction func vistorLogin(_ sender: UIButton) {
        navigationController?.dismiss(animated: true)
    }
}

private extension Reactive where Base: UILabel {
    var isValid: Binder<(Bool, String)> {
        return Binder<(Bool, String)>(base, binding: { (label, valid) in
            label.text = valid.1
            label.textColor = valid.0 ? .green : .red
        })
    }
}
